package com.toe.user.webapi.service.impl;

import com.toe.pojo.dto.user.UserRolePermissionAddDTO;
import com.toe.pojo.entity.user.UserAdmin;
import com.toe.pojo.entity.user.UserRolePermission;
import com.toe.pojo.vo.user.UserAdminDetails;
import com.toe.user.service.UserRolePermissionService;
import com.toe.user.service.UserRoleService;
import com.toe.user.webapi.mapper.UserAdminMapper;
import com.toe.user.service.UserAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.toe.user.webapi.mapper.UserRolePermissionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author 陶涛
 * @since 2022-07-19
 */
@Service
@Slf4j
public class UserAdminServiceImpl extends ServiceImpl<UserAdminMapper, UserAdmin> implements UserAdminService {

    @Autowired
    private UserAdminMapper userAdminMapper;

    @Autowired
    private UserRolePermissionService userRolePermissionService;

    @Autowired
    private UserRolePermissionMapper userRolePermissionMapper;

    @Override
    public void delete(long id) {
        userAdminMapper.deleteAdmin(id);
    }

    @Override
    public void unDelete(long id) {
        userAdminMapper.unDeleteAdmin(id);
    }

    @Override
    public List<UserAdminDetails> selectAllAdmin() {
        List<UserAdminDetails> userAdminDetails = userAdminMapper.selectAllAdmin();
        return userAdminDetails;
    }

    @Override
    public void addAdminPermission(long roleId,long[] permissionIds) {
        //获取到新的角色权限,先删除老权限
       userRolePermissionMapper.deleteByRoleId(roleId);
        //添加新权限
        for (int i = 0; i < permissionIds.length; i++) {
            UserRolePermissionAddDTO userRolePermissionAddDTO = new UserRolePermissionAddDTO();
            userRolePermissionAddDTO.setPermissionId(permissionIds[i]);
            userRolePermissionAddDTO.setRoleId(roleId);
            userRolePermissionService.insert(userRolePermissionAddDTO);
        }
        log.info("结束!!!");
    }
}
